package com.example.communitypro.controller;


import com.example.communitypro.pojo.Notification;
import com.example.communitypro.service.NotificationService;
import com.example.communitypro.service.UserActionsService;
import com.example.communitypro.pojo.UserActions;
import com.example.communitypro.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/userAction")
public class UserActionController {
    @Autowired
    private UserActionsService userActionsService;

    @Autowired
    private NotificationService notificationService;

    @ResponseBody
    @RequestMapping(value = "/addUserAction")
    @CrossOrigin
    public Result addUserAction(UserActions record){
        return userActionsService.addUserActions(record);
    }
    @ResponseBody
    @RequestMapping(value = "/dropUserAction")
    @CrossOrigin
    public Result dropUserAction(Integer user_action_no){
        return userActionsService.dropUserActions(user_action_no);
    }

    @ResponseBody
    @RequestMapping(value = "/modifyUserAction")
    @CrossOrigin
    public Result modifyUserAction(UserActions record){
        return userActionsService.modifyUserActions(record);
    }

    @ResponseBody
    @RequestMapping(value = "/queryUserAction")
    @CrossOrigin
    public Result queryUserAction(Integer user_action_no){
        return userActionsService.queryUserActions(user_action_no);
    }


    @ResponseBody
    @RequestMapping(value = "/queryUserFollow")
    @CrossOrigin
    public Result queryUserFollow(Integer user_no, Integer user_action_type){
        return userActionsService.queryUserFollow(user_no,user_action_type);
    }

    @ResponseBody
    @RequestMapping(value = "/UserAction")
    @CrossOrigin
    public Result UserAction(UserActions record){
        Notification notification1 = new Notification();
        notification1.setNotificationDate(new Date());

        if(record.getUserActionType()==1){
            notification1.setNotificationType(0);
            notification1.setPostNo(record.getUserObjectNo());
            notification1.setUserNo(record.getUserNo());
            notificationService.addNotification(notification1);
        }
        return userActionsService.UserAction(record);
    }

    @ResponseBody
    @RequestMapping(value = "/JudgeUserAction")
    @CrossOrigin

    public Result JudgeUserAction(UserActions record){return userActionsService.judgeUserAction(record);}



}
